package cn.com.dhcc.ddi.node.service;

import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.BaseService;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.pub.CM;
import cn.com.dhcc.ddi.node.dao.DataCategoryDao;
import cn.com.dhcc.ddi.node.vo.DataCategory;
import cn.com.dhcc.ddi.node.vo.wapper.DataCategoryRowWapper;
/**
 * 
    * @ClassName: DataCategoryService
    * @Description: 数据分类Service
    * @author 18315
    * @date 2017-3-10
    *
 */
@Service
public class DataCategoryService extends BaseService {
	private static Logger logger = Log.getLogger(LOGTYPE.DDI);
	@Autowired
	private DataCategoryDao dataCategoryDao;
	
	/**
	 * 
	    * @Title: list
	    * @Description: 数据分类分页信息列表
	    * @param @param pager
	    * @param @param condition
	    * @param @return
	    * @param @throws ServiceException    参数
	    * @return PagerData<DataCategory>    返回类型
	    * @throws
	 */
	public PagerData<DataCategory> DataCategorylist(Pager pager, WhereCondition condition) throws ServiceException {
		try {
			return dataCategoryDao.list(pager, condition);
		} catch (DaoException e) {
			throw new ServiceException("获取数据分类分页信息列表失败", e);
		}
	}
	
	/**
	 * 根据业务域id获取数据分类信息列表
	 * @return 数据分类信息列表
	 * @throws ServiceException
	 */
	public List<DataCategory> getDcListByDomainId(String domainId) throws ServiceException {
		try {
			return dataCategoryDao.getDcListByDomainId(domainId);
		} catch (DaoException e) {
			throw new ServiceException("根据业务域id获取数据分类信息列表失败", e);
		}
	}
	
	/**
	 * 根据业务域id获取可用数据分类信息列表
	 * @return 数据分类信息列表
	 * @throws ServiceException
	 */
	public List<DataCategory> getUsableDcListByDomainId(String domainId) throws ServiceException {
		try {
			return dataCategoryDao.getUsableDcListByDomainId(domainId);
		} catch (DaoException e) {
			throw new ServiceException("根据业务域id获取可用数据分类信息列表失败", e);
		}
	}
	
	/**
	 * 更新数据分类信息
	    * @Title: insert
	    * @Description: TODO(这里用一句话描述这个方法的作用)
	    * @param @param vo
	    * @param @throws ServiceException
	    * @param @throws NiceException    参数
	    * @return void    返回类型
	    * @throws
	 */
	public int insert(DataCategory vo) throws ServiceException {
		
		try {
			return dataCategoryDao.insert(vo);
		} catch (DaoException e)  {
			throw new ServiceException("新增失败", e);
		}
	}
	public int update(DataCategory vo) throws ServiceException {
		
		try {
			return dataCategoryDao.update(vo);
		}catch (DaoException e)  {
			throw new ServiceException("更新失败", e);
		}
	}
	/**
	 * 修改或者更新
	    * @Title: insertOrUpdate
	    * @Description: TODO(这里用一句话描述这个方法的作用)
	    * @param @param obj
	    * @param @throws DaoException    参数
	    * @return void    返回类型
	    * @throws
	 */
	public int insertOrUpdate(DataCategory obj) throws ServiceException {
		try {
		    return dataCategoryDao.insertOrUpdate(obj);
		} catch (DaoException e) {
			throw new ServiceException("更新任务失败", e);
		}
	}
	/**
	 * @throws ServiceException 
	 * 获取对象
	    * @Title: get
	    * @Description: TODO(这里用一句话描述这个方法的作用)
	    * @param @param id
	    * @param @return
	    * @param @throws DaoException    参数
	    * @return NodeInfo    返回类型
	    * @throws
	 */
	public DataCategory get(String id) throws ServiceException {
		try{
			return dataCategoryDao.get(id);			
		} catch (DaoException e) {
			throw new ServiceException("查询失败", e);
		}
	}
	
	/**
	 *
	    * @Title: delete
	    * @Description:  删除
	    * @param @param status
	    * @param @param id
	    * @param @return    参数
	    * @return boolean    返回类型
	    * @throws
	 */
	public boolean delete(String status, String id) {
		boolean result = false;
		String sql = "delete from " + SI_TABLE("t_ddi_data_catagory") 
				+ " WHERE id=?";
		try {
			result = CM.getDao().delete("删除", sql, new Object[]{id})> 0 ? true : false;
		} catch (DaoException e) {
			new ServiceException("删除失败", e);
		}
		return result;
	}
	/**
	 * 
	    * @Title: batchDelete
	    * @Description: 批量删除(备用)
	    * @param @param ids
	    * @param @return
	    * @param @throws ServiceException    参数
	    * @return int    返回类型
	    * @throws
	 */
	public int batchDelete(String[] ids) throws ServiceException {
		try {
			return dataCategoryDao.batchDelete(ids);
		} catch (DaoException e) {
			throw new ServiceException("失败", e);
		}
	}
	
	/**
	 * 验证名称唯一性
	 * @param name
	 * @param checkStr
	 * @return
	 * @throws ServiceException
	 */
	
	
	public boolean isPropertyUnique(String propertyName, String newValue, String oldValue) throws ServiceException {
		try {
			if (newValue.equals(oldValue)) {
				return true;
			}
			WhereCondition searchParameter = new WhereCondition();
			searchParameter.where().eq(propertyName, newValue);
			return dataCategoryDao.count(searchParameter) == 0;
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 获取数据分类对应业务域id
	 * @param  dataCatagoryId
	 * @return 业务域id
	 * @throws ServiceException
	 */  
	public String getDomainId(String dataCatagoryId) throws ServiceException {
		try {
			DataCategory dc = dataCategoryDao.get(dataCatagoryId);
		    return dc.getDomain_id();
		} catch (DaoException e) {
			throw new ServiceException("获取数据分类对应业务域id失败", e);
		}
	}
	

	/**
	 * 根据id 停用或启用
	 * @param 
	 * @param status 状态 0：停用  1：启用
	 * @return true 成功   false 失败
	 * @throws DaoException 
	 */
	public boolean del(String id, String status) {
		boolean result = false;
		String sql = "UPDATE " + SI_TABLE("t_ddi_data_catagory") + " SET status=?"
				+ " WHERE id=?";
		try {
			result = CM.getDao().update("更新dataCategory", sql, new Object[]{status, id}) > 0 ? true : false;
		} catch (DaoException e) {
			new ServiceException("更新dataCategory失败", e);
		}
		return result;
	}
	
	/**
	 * 获取全部停用数据分类
	 * 
	 * @return
	 * @throws DaoException
	 */
	public List<DataCategory> getAlldataCategoryList() throws DaoException {
		try {
			final String sql = "select * from "
					+ SI_TABLE("t_ddi_node_info")
					+ " where  status <>'1'";
			return CM.getDao().list("获取全部数据分类", sql, new DataCategoryRowWapper(),
					new Object[] {});
		} catch (DaoException e) {
			throw new DaoException("获取全部停用数据分类失败！", e);
		}
	}
	
}
